import { app } from 'electron';
import path from 'path';

import { log } from './logger';

/**
 * 图标管理类
 */
export class IconManager {
  private static basePath: string = path.join(app.getAppPath(), 'resources');

  /**
   * 获取应用图标路径
   */
  static getAppIcon(): string {
    const iconPath = path.join(this.basePath, process.platform === 'win32' ? 'logo.ico' : 'logo.png');
    log.info('应用图标路径:', iconPath);
    return iconPath;
  }

  /**
   * 获取托盘图标路径
   */
  static getTrayIcon(): string {
    const iconPath = path.join(this.basePath, process.platform === 'win32' ? 'tray-icon.ico' : 'tray-icon.png');
    log.info('托盘图标路径:', iconPath);
    return iconPath;
  }

  /**
   * 检查图标文件是否存在
   */
  static checkIconExists(iconPath: string): boolean {
    try {
      const fs = require('fs');
      return fs.existsSync(iconPath);
    } catch (error) {
      log.error('检查图标文件失败:', error);
      return false;
    }
  }

  /**
   * 验证所有图标文件
   */
  static validateIcons(): { app: boolean; tray: boolean } {
    const appIcon = this.getAppIcon();
    const trayIcon = this.getTrayIcon();

    const appExists = this.checkIconExists(appIcon);
    const trayExists = this.checkIconExists(trayIcon);

    if (!appExists) {
      log.warn('应用图标文件不存在:', appIcon);
    }
    if (!trayExists) {
      log.warn('托盘图标文件不存在:', trayIcon);
    }

    return {
      app: appExists,
      tray: trayExists
    };
  }
}

// 导出便捷方法
export const getAppIcon = () => IconManager.getAppIcon();
export const getTrayIcon = () => IconManager.getTrayIcon();
export const validateIcons = () => IconManager.validateIcons();
